package com.engine.update.cmd;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.dao.CodeResourceMapper;
import com.engine.core.domain.GenTable;
import com.engine.core.domain.GenTableColumn;
import com.engine.core.domain.JavaFunction;
import com.engine.core.gen.ParamOption;
import com.engine.core.interceptor.JdbcCommandContext;
import com.engine.core.interceptor.ResponseCommand;
import com.engine.core.rowMapper.JdbcTypeBiz;
import com.engine.core.rowMapper.MappedType;
import com.engine.core.util.JdbcTemplateUtils;
import com.engine.core.util.QueryUtil;
import com.engine.core.util.Response;
import com.engine.core.util.Utils;
import com.engine.system.User;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class UpdateMultiCommand extends ResponseCommand {

    public UpdateMultiCommand(User user, Map<String, Object> params) {
        super(user, params);
    }

    /**
     * @param commandContext
     * @return
     * @throws Exception
     */
    @Override
    public Response execute(JdbcCommandContext commandContext) throws Exception {
        Response response=Response.static_success("执行成功!");
        try {
            boolean isbatch= Utils.nullDefault((String)params.get(QueryUtil.UPDATA_IS_BATCH),"0").equals("1");
            params.put(QueryUtil.QUERY_PARAMS_URLENCODE,"0");
            ParamOption paramOption=new ParamOption(params);
            JSONObject paramRoot=JSONObject.parseObject((String)params.get(QueryUtil.QUERY_CONTENT));
            CodeResourceMapper codeResourceMapper=commandContext.getApplicationContext().getBean(CodeResourceMapper.class);
            JdbcTemplate jdbcTemplate=commandContext.getJdbcTemplate();
            String columns= paramRoot.getString(QueryUtil.QUERY_COLUMNS);
            String whereColumns= paramRoot.getString(QueryUtil.QUERY_WHERE);
            String datas=paramRoot.getString(QueryUtil.UPDATA_DATA);

            String resourceId=Utils.urlDecode(paramRoot.getString(QueryUtil.QUERY_RESOURCE),paramOption);
            String[] dateformat=commandContext.getGlobal().getDateformat();
            GenTable genTable=codeResourceMapper.getGenTable(Utils.getInt(resourceId,-1));

            Map<String, GenTableColumn> columnTypeMap=new HashMap<>();
            Iterator<GenTableColumn> columnListIterator=genTable.getColumns().iterator();
            while(columnListIterator.hasNext()){
                GenTableColumn genTableColumn=columnListIterator.next();
                columnTypeMap.put(genTableColumn.getColumnName(),genTableColumn);
            }


            Map<String, JavaFunction> functionMap=new LinkedHashMap<>();
            Map<String,String> othersMap=new HashMap<>();
            LinkedHashMap<String, MappedType> mappedTypeMap= JdbcTypeBiz.getUpdateParamsTypeByDB(columns,datas,columnTypeMap,functionMap,othersMap);

            Map<String,String> whereOthersMap=new HashMap<>();
            LinkedHashMap<String, MappedType> whereMappedTypeMap = JdbcTypeBiz.getUpdateParamsTypeByDB(whereColumns,
                    datas,columnTypeMap,functionMap,whereOthersMap);


            LinkedHashMap<String, MappedType> allMappedTypeMap=new LinkedHashMap<>(mappedTypeMap);
            allMappedTypeMap.putAll(whereMappedTypeMap);

            JdbcTypeBiz.filterColumns(columnTypeMap,mappedTypeMap);
            JdbcTypeBiz.filterColumns(columnTypeMap,whereMappedTypeMap);


            List<Map<String,Object>> dataList= JdbcTypeBiz.getUpdateDataByDB(datas,dateformat,allMappedTypeMap,functionMap);
            response.set(QueryUtil.QUERY_PARAMS,new ArrayList<>(dataList));
            response.set(QueryUtil.QUERY_PARAMS+"_types",new HashMap<>(mappedTypeMap));


            if(isbatch){
                Response res= JdbcTemplateUtils.updateMultiData(genTable,jdbcTemplate, dataList,mappedTypeMap,othersMap,whereMappedTypeMap,whereOthersMap,true);
                response.setMapValues(res.getMapValues());
            }else{
                Response res=JdbcTemplateUtils.updateMultiData(genTable,jdbcTemplate,dataList,mappedTypeMap,othersMap,whereMappedTypeMap,whereOthersMap,false);
                response.setMapValues(res.getMapValues());
            }
        } catch (Exception e) {
            response.setErr(e);
        }
        return response;
    }
}
